import time


def get_time(func):
    """
    :param func:#被装饰的函数不带参数
    :return:
    """
    def wrapper():
        startTime = time.time()
        func()
        endTime = time.time()
        processTime = (endTime - startTime) * 1000
        print("The function timing is %f ms" % processTime)
    return wrapper


def get_time2(func):
    """
    :param func:被装饰的函数带参数
    :return:
    """
    def wrapper(*args, **kwargs):
        startTime = time.time()
        func(*args, **kwargs)
        endTime = time.time()
        processTime = (endTime - startTime) * 1000
        print("This function timing is %f ms" % processTime)
    return wrapper


def ui_log(func):
    """
    :param func: 被装饰的函数带参数
    :return:
    """
    def wrapper(*args, **kwargs):
        start_time = time.time()
        func(*args, **kwargs)
        end_time = time.time()
        process_time = (end_time - start_time) * 1000
        print("This function timing is {}".format(process_time))
    return wrapper


@get_time2
def myfunc2(a):
    print("start func")
    print(a)
    time.sleep(0.8)
    print("end func")


@ui_log
def myfunc(cpu_num):
    print("start func")
    time.sleep(0.8)
    print("end func {}".format(cpu_num))


def catch_exception(func):
     def wrapper(*args, **kwargs):
          try:
           func(*args, **kwargs)
           print("time :{}".format(time.time()))
          except Exception as e:
              print("e"+e)
     return wrapper


class Test(object):
    def __init__(self):
        pass

    @catch_exception
    def read_value(self, flag):
        print('here I will do something.{}'.format(flag))


def test_three():
    # return "sb" if name == "allen" else "cnm"
    # res_list = ["case id: {}".format(i) for i in range(20) if i % 2 is 0]    # 三元运算列表推导式
    res_list = ("case id: {}".format(i) for i in range(20) if i % 2 is 0)      # 生成器
    return res_list


def test_case():
    """
    :return:上下文管理
    """
    print("不使用上下文管理器")
    print("*" * 30)
    f = open('file.py', 'w')
    print(f.closed)
    f.write("# Hello World")
    f.close()
    print(f.closed)

    print("\n使用上下文管理器")
    print("*" * 30)
    with open("file.py", 'w') as f:
        f.write('# Hello Python')


def test_for_enumerate():
    classes = ['dog', 'cat', 'fish']
    classes = "afdsfdadsf"
    classes = ("dog", "cat", "fish", "cato")
    classes = {"dog": "fafsf", "cat":"rwqe", "fish":"asdfsfsdfsd"}

    for index, item in enumerate(classes):
        format_tuple = (index, item)
        print("i:{}, cls:{}".format(format_tuple[0], format_tuple[1]))

    # 如果要统计文件的行数，可以这样写：
    filepath = "D:\一人一档_修改后数据\人像库_new"
    count = len(open(filepath, 'r').readlines())
    print(count)
    # 这种方法简单，但是可能比较慢，当文件比较大时甚至不能工作。

    # 可以利用enumerate()：
    count = 0
    for index, line in enumerate(open(filepath, 'r')):
        count += 1
    print("count: {}".format(count))


def test_case_method():
        pass


if __name__ == '__main__':
    test_case_method()
